﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Simple.Services;
namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 应用管理
/// </summary>
[Route("QHSEOrganization/[action]")]
[ApiController]

public class QHSEOrganizationController : ControllerBase
{
    private readonly QHSEOrganizationService _organizationService;

    public QHSEOrganizationController(QHSEOrganizationService organizationService)
    {
        _organizationService = organizationService;
    }

    /// <summary>
    /// 组织列表
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> List()
    {
        List<QHSEOrganizationModel> data = await _organizationService.GetAsync();
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// id取值
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> GetValue([FromBody] IdInputModel model)
    {
        var data = await _organizationService.GetByIdAsync(model);
        return AppResult.Status200OK(data: data);
    }

    [HttpGet]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> TreeList([FromQuery] QHSEOrganizationInputModel input)
    {
        List<QHSEOrganizationTreeNodeModel> data = await _organizationService.GetAsync(input);
        return AppResult.Status200OK(data: data);
    }

    [HttpPost]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> Add([FromBody] QHSEOrganizationModel model)
    {
        await _organizationService.AddAsync(model);
        return AppResult.Status200OK("新增成功");
    }
    /// <summary>
    /// 组织树
    /// </summary>
    /// <returns></returns>
    [HttpPost]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> Tree([FromBody] IdInputModel idInputModel)
    {
        List<AntTreeNode> data = new List<AntTreeNode>();
        if (idInputModel.Id == "安检院" || idInputModel.Id == "长庆监督公司" || idInputModel.Id == "机关总部" || idInputModel.Id == "越盛公司" || idInputModel.Id == "苏里格项目经理部"
           || idInputModel.Id == "页岩气工程项目部" || idInputModel.Id == "页岩气项目经理部")
        {
            data = await _organizationService.GetTreeAsync();
            return AppResult.Status200OK(data: data);
        }
        data = await _organizationService.GetTreeAsync(idInputModel);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 获取长庆井下技术作业公司组织树
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> SecondaryOrganizationTree()
    {
        List<AntTreeNode> data = new List<AntTreeNode>();
        var orgName = new IdInputModel();
        orgName.Id = "长庆井下技术作业公司";
        data = await _organizationService.GetTreeAsync(orgName);
        return AppResult.Status200OK(data: data);
    }
    /// <summary>
    /// 获取长庆钻井总公司组织树
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    [ValidateTimestampAndSignature]
    public async Task<AppResult> SecondaryOrganizationTree1()
    {
        List<AntTreeNode> data = new List<AntTreeNode>();
        var orgName = new IdInputModel();
        orgName.Id = "长庆钻井总公司";
        data = await _organizationService.GetTreeAsync(orgName);
        return AppResult.Status200OK(data: data);
    }

    ///// <summary>
    ///// 组织删除
    ///// </summary>
    ///// <param name="models"></param>
    ///// <returns></returns>
    [HttpDelete]
    [CustomAuthorize("hdv_qhse_data")]
    public async Task<AppResult> Delete([FromBody] List<IdInputModel> model)
    {
        await _organizationService.DeleteAsync(model.Select(m => m.Id));
        return AppResult.Status200OK("删除成功");
    }
}

